package com.labichaoka.chaoka.ui.bank.bind;

import com.labichaoka.chaoka.base.MyApplication;
import com.labichaoka.chaoka.entity.BankInfoDtoResponse;
import com.labichaoka.chaoka.entity.BaseResponse;
import com.labichaoka.chaoka.entity.BindBankRequest;
import com.labichaoka.chaoka.entity.GetBankSmsRequest;
import com.labichaoka.chaoka.entity.IDCardResponse;
import com.labichaoka.chaoka.entity.SwitchCardResponse;
import com.labichaoka.chaoka.net.BaseSubscriber;
import com.labichaoka.chaoka.net.DataManager;

import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;
import rx.subscriptions.CompositeSubscription;

/**
 * Created by ruan on 2018/8/8.
 */

public class BankBindInteractorImpl implements BankBindInteractor{
    private DataManager manager = new DataManager(MyApplication.mContext);
    private CompositeSubscription mCompositeSubscription = new CompositeSubscription();

    @Override
    public void getSmsCode(GetBankSmsRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.getBankSms(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onGetSmsCodeFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onGetSmsCodeSuccessed();
                    }
                })



        );

    }

    @Override
    public void bindCard(GetBankSmsRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.bindCard(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onBindCardFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onBindCardSuccessed(response);
                    }
                })



        );
    }

    @Override
    public void bindCardVail(BindBankRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.bindCardVail(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onBindCardVailFailed();
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                        listener.onBindCardVailSucc(response);
                    }
                })



        );
    }

    @Override
    public void getIdCardName(OnFinishedListener listener) {
        mCompositeSubscription.add(manager.queryIDCard()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<IDCardResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onGetIDCardNameFailed();
                    }

                    @Override
                    public void onNext(IDCardResponse response) {
                        super.onNext(response);
                        listener.onGetIDCardNameSuccessed(response);
                    }
                })



        );
    }

    @Override
    public void getBankCardList(OnFinishedListener listener) {
        mCompositeSubscription.add(manager.getBankCardList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BankInfoDtoResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onGetBankCardListFailed();
                    }

                    @Override
                    public void onNext(BankInfoDtoResponse response) {
                        super.onNext(response);
                        listener.onGetBankCardListSuccessed(response);
                    }
                })
        );

    }

    @Override
    public void behavior(String actionNode) {
        mCompositeSubscription.add(manager.behavior(actionNode)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<BaseResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                    }

                    @Override
                    public void onNext(BaseResponse response) {
                        super.onNext(response);
                    }
                })
        );
    }

    @Override
    public void scrdSwitchCard(GetBankSmsRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.scrdSwitchCard(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<SwitchCardResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onScrdSwitchCardFailed();
                    }

                    @Override
                    public void onNext(SwitchCardResponse response) {
                        super.onNext(response);
                        listener.onScrdSwitchCardSucc(response);
                    }
                })
        );
    }

    @Override
    public void submitSwitchCard(BindBankRequest request, OnFinishedListener listener) {
        mCompositeSubscription.add(manager.submitSwitchCard(request)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseSubscriber<SwitchCardResponse>() {

                    @Override
                    public void onError(Throwable e) {
                        super.onError(e);
                        listener.onSubmitSwitchCardFailed();
                    }

                    @Override
                    public void onNext(SwitchCardResponse response) {
                        super.onNext(response);
                        listener.onSubmitSwitchCardSucc(response);
                    }
                })
        );
    }
}
